package autorization;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
 * @author Volk ID = 1001
 * The class is elementary record in the autorization table
 */
class RecordThreadTable{
   //-----------Record attributes---------------
   private Object rec[]; //data structute for storing fields of record
     private int nCol; //count fields in record
     private Lock readLock; //Locking on reading
     private Lock writeLock; //Locking on writing
   //------------------------------------------
   //-----------Сервисные методы---------------
   /**
    * Check on occurrence of an entry index in a range of admissible values
    * @param rangeValue - entry index
    * @return true - Admissible values range to content of entry index
    *         false - Admissible values range don't content of entry index
    */
   private boolean __ASSERT(int rangeValue){
    if( (rangeValue >= 0) && (rangeValue < nCol)) return true; 
    else return false; 
   }
   //------------------------------------------
   public RecordThreadTable(int nCol){
    rec = new Object[nCol]; 
    this.nCol = nCol; 
    ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); 
      readLock = rwl.readLock(); 
      writeLock = rwl.writeLock(); 
   }   
   //-------------Разработка интерфейсных методов-------------
     /**
      *To set data on a entry index
      * @param data - entry data
      * @param index - entry index
      * @return false -entry parametres is non-correct
      *         true - operation was carried out
      */
     public boolean set(Object data, int index){
      if( (!__ASSERT(index)) && (data == null)) return false; 
      writeLock.lock();
      try{
      rec[index] = data;  
      }finally{
       writeLock.unlock(); 
      }
      return true; 
     }
     
     /**
      * To get data on a entry index
      * @param index - entry index
      * @return data on a established index
      * @throws java.lang.ArrayIndexOutOfBoundsException - entry index is not content in admissible values range
      */
     public Object get(int index) throws ArrayIndexOutOfBoundsException{
     if(!__ASSERT(index)) throw new ArrayIndexOutOfBoundsException();  
     readLock.lock();
     try{
      return rec[index];
     }finally{
      readLock.unlock(); 
     }     
     }
   //---------------------------------------------------------
   
}